package com.leyou.search.web;

import com.leyou.search.dto.FilterListDto;
import com.leyou.search.dto.SearchParamDTO;
import com.leyou.search.entity.Goods;
import com.leyou.search.service.SearchService;
import com.leyou.starter.elastic.dto.PageInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import java.util.List;


@RestController
@RequestMapping("goods")
public class SearchController {

    private final SearchService searchService;

    public SearchController(SearchService searchService) {
        this.searchService = searchService;
    }

    /**
     * 初始化索引库
     */
    @GetMapping("initialization")
    public ResponseEntity<String> init() {
        //创建索引库
        searchService.createIndexAndMapping();
        //导入数据
        searchService.loadData();
        return ResponseEntity.ok("导入成功");
    }

    //关键字补全
    @GetMapping("/suggestion")
    public Mono<List<String>> getSuggestion(@RequestParam("key") String key) {
        System.out.println("key = " + key);
        return searchService.getSuggestion(key);
    }

    @PostMapping("/list")
    public Mono<PageInfo<Goods>> searchGoods(@RequestBody SearchParamDTO request){
        System.out.println("------------->goods/list");
        return searchService.searchGoods(request);
    }
//
//    @PostMapping("/filter")
//    public Mono<FilterListDto> find(){
//
//    }
}